using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Core.BOL;
using Core.DAL;
using Core.Extentions;
using Core.Utils;
using ScoutNET.ActionsResults;
using System.Security.Permissions;

namespace ScoutNET.Controllers
{
    public class StaffController : ApplicationController
    {
        private StaffRepository sr = new StaffRepository();
        private SectionRepository ser = new SectionRepository();

        #region View
        //
        // GET: /Section/Staff/Index
        public ActionResult Index(string systemName)
        {
            // Meta Tag
            string section = ser.GetSectionNameBySystemName(systemName);
            ViewData["PageTitle"] = Resources.Staff_Index_Title.Replace("[SECTION]", section.ToLower());
            ViewData["PageDescription"] = Resources.Staff_Index_Description.Replace("[SECTION]", section.ToLower());
            ViewData["ContactStaffLink"] = Resources.Staff_Index_ContactLink.Replace("[SECTION]", section.ToLower());

            UserType ut = UsersRepository.GetUserType(systemName);
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            //Breadcrumb
            string[] saBreadcrumb = new String[2];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section.ToLower());
            ViewData["Breadcrumb"] = saBreadcrumb;

            int startYear = sr.GetFirstYearBySystemName(systemName);
            int endYear = sr.GetLastYearBySystemName(systemName);

            Dictionary<int, string> dYear;
            if (endYear != -1 && startYear != -1 && startYear != endYear)
            {
                endYear = (endYear == DateTime.Now.GetNowTimeByTimeZone().Year) ? endYear - 1 : endYear;
                dYear = new Dictionary<int, string>();
                dYear.Add(0, " ");
                for (int i = endYear; i >= startYear; i--)
                {
                    dYear.Add(i, i.ToString() + " - " + (i + 1).ToString());
                }
                ViewData["SlArchivesDates"] = new SelectList(dYear, "Key", "Value");
            }

            List<tSite_Staff> ls = sr.GetStaffBySystemName(systemName);
            if (ls.Count == 0)
            {
                ViewData["ErrorMessage"] = Resources.Staff_Create_Error_NoItem;
                return View("Error");
            }
            else
            {
                return View("Index", ls);
            }
        }

        //
        // GET: /Section/Staff/Archives/0000
        public ActionResult Archives(string systemName, int? id)
        {
            int currentYear = (id != null) ? (int)id : DateTime.Now.GetNowTimeByTimeZone().Year;
            UserType ut = UsersRepository.GetUserType(systemName);
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            string section = ser.GetSectionNameBySystemName(systemName);
            ViewData["PageTitle"] = Resources.Staff_Archives_Title.Replace("[SECTION]", section.ToLower()).Replace("[YEAR]", currentYear.ToString() + " / " + (currentYear + 1).ToString());
            ViewData["PageDescription"] = Resources.Staff_Archives_Description.Replace("[SECTION]", section.ToLower()).Replace("[YEAR]", currentYear.ToString() + " - " + (currentYear + 1).ToString());
            ViewData["ContactStaffLink"] = Resources.Staff_Index_ContactLink.Replace("[SECTION]", section.ToLower());
            ViewData["SystemName"] = systemName;

            string[] saBreadcrumb = new String[3];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section.ToLower()), "SectionIndex", "Index", "Staff", new RouteValueDictionary(new { systemName = systemName }), null);
            saBreadcrumb[2] = Resources.Staff_Archives_Breadcrumb.Replace("[YEAR]", currentYear.ToString() + " / " + (currentYear + 1).ToString());
            ViewData["Breadcrumb"] = saBreadcrumb;

            int startYear = sr.GetFirstYearBySystemName(systemName);
            int endYear = sr.GetLastYearBySystemName(systemName);

            Dictionary<int, string> dYear;
            if (endYear != -1 && startYear != -1)
            {
                endYear = (endYear == DateTime.Now.GetNowTimeByTimeZone().Year) ? endYear - 1 : endYear;
                dYear = new Dictionary<int, string>();
                dYear.Add(0, " ");
                for (int i = endYear; i >= startYear; i--)
                {
                    dYear.Add(i, i.ToString() + " - " + (i + 1).ToString());
                }
                ViewData["SlArchivesDates"] = new SelectList(dYear, "Key", "Value", currentYear);
            }
            ViewData["ArchiveYear"] = currentYear;


            List<tSite_Staff> ls = sr.GetStaffBySystemNameAndYear(systemName, currentYear);
            if (ls.Count == 0)
            {
                ViewData["ErrorMessage"] = Resources.Staff_Create_Error_NoItem;
                return View("Error");
            }
            else
            {
                return View("Archives", ls);
            }
        }

        //
        // GET: /Section/Staff/Create
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Create(string systemName)
        {
            // Redirect to admin section
            string adminSection = UsersRepository.GetAdminSection(systemName);
            UserType ut = UsersRepository.GetUserType(systemName);
            if (systemName != adminSection && ut != UserType.Administrator)
            {
                return RedirectToAction("Create", new { systemName = adminSection });
            }
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            // Meta Tag
            string section = ser.GetSectionNameBySystemName(systemName).ToLower();
            ViewData["PageTitle"] = Resources.Staff_Create_Title.Replace("[SECTION]", section);
            ViewData["PageDescription"] = Resources.Staff_Create_Description;

            //Breadcrumb
            string[] saBreadcrumb = new String[3];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section), "SectionIndex", "Index", "Staff", new RouteValueDictionary(new { systemName = systemName }), null);
            saBreadcrumb[2] = Resources.Staff_Create_Breadcrumb.Replace("[SECTION]", section);
            ViewData["Breadcrumb"] = saBreadcrumb;

            ViewData["SlDateIn"] = new SelectList(GetListYear(false), "Key", "Value");
            ViewData["SlDateOut"] = new SelectList(GetListYear(true), "Key", "Value");
            ViewData["SectionName"] = systemName;
            ViewData["MaxRequestLength"] = Files.GetMaxRequestLength();
            ViewData["ImageStaff"] = null;
            ViewData["FormsToken"] = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewData["DefaultImageID"] = Guid.NewGuid().ToString();

            Session["baImageStaff"] = null;

            return View();
        }

        //
        // Post: /Section/Staff/Create
        [HttpPost]
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Create(string systemName, FormCollection fc)
        {
            // Redirect to admin section
            string adminSection = UsersRepository.GetAdminSection(systemName);
            UserType ut = UsersRepository.GetUserType(systemName);
            if (systemName != adminSection && ut != UserType.Administrator)
            {
                return RedirectToAction("Edit", new { systemName = adminSection });
            }
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            // Meta Tag
            tSite_Section section = ser.GetSectionsBySystemName(systemName);
            ViewData["PageTitle"] = Resources.Staff_Create_Title.Replace("[SECTION]", section.Section);
            ViewData["PageDescription"] = Resources.Staff_Create_Description;

            //Breadcrumb
            string[] saBreadcrumb = new String[3];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section.Section), "SectionIndex", "Index", "Staff", new RouteValueDictionary(new { systemName = systemName }), null);
            saBreadcrumb[2] = Resources.Staff_Create_Breadcrumb.Replace("[SECTION]", section.Section);
            ViewData["Breadcrumb"] = saBreadcrumb;

            if (ValidateNewStaffMember(fc))
            {
                Guid? imageId = null;
                if (HttpContext.Session["baImageStaff"] != null)
                {
                    imageId = Guid.NewGuid();
                    string imagePath = this.ControllerContext.HttpContext.Server.MapPath(ConfigurationManager.AppSettings["StaffPath"] + imageId + ".jpg");
                    System.IO.File.WriteAllBytes(imagePath, (byte[])HttpContext.Session["baImageStaff"]);
                }

                tSite_Staff member = new tSite_Staff();
                member.Image = imageId;
                member.IsModerator = (fc["isModerator"] == "1") ? true : false;
                member.Lastname = fc["lastname"];
                member.Mobile = fc["mobile"];
                member.Phone = fc["phone"];
                member.Sex = char.Parse(fc["sex"]);
                member.SID = section.ID;
                member.Totem = fc["totem"];
                member.Firstname = fc["firstname"];
                member.Email = fc["email"];
                member.DateIn = int.Parse(fc["SlDateIn"]);
                sr.InsertStaff(member);
                sr.Save();

                ViewData["MID"] = member.ID;
                ViewData["ValidationMessage"] = ScoutNET.Resources.Staff_Create_Validation.Replace("[SECTION]", section.Section.ToLower());
                HttpContext.Session["baImageStaff"] = null;

                List<tSite_Staff> staff = sr.GetStaffBySystemName(systemName);
                staff.Remove(member);
                return View("Validation", staff);
            }
            else
            {
                ViewData["SlDateIn"] = new SelectList(GetListYear(false), "Key", "Value");
                ViewData["SlDateOut"] = new SelectList(GetListYear(true), "Key", "Value");
                ViewData["SectionName"] = systemName;
                ViewData["ImageStaff"] = HttpContext.Session["baImageStaff"];
                ViewData["FormsToken"] = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
                ViewData["MaxRequestLength"] = Files.GetMaxRequestLength();
                ViewData["DefaultImageID"] = Guid.NewGuid().ToString();

                return View();
            }
        }

        //
        // Post: /Section/Staff/Archive
        [HttpPost]
        [Authorize(Roles = "Animateur,C.U")]
        public void Archive(FormCollection fc)
        {
            int id = int.Parse(fc["ID"]);
            
            tSite_Staff s = sr.GetStaffMemberByID(id);
            s.DateOut = DateTime.Now.GetNowTimeByTimeZone().Year;
            sr.Save();
        }

        //
        // Post: /Section/Staff/Archive/0000
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Archive(int id)
        {
            ViewData["Message"] = Resources.Staff_ConfirmArchives_Message;
            ViewData["ID"] = id.ToString();
            return View("Confirm");
        }

        //
        // Post: /Section/Staff/Delete/0000
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Delete(int id)
        {
            ViewData["Message"] = Resources.Staff_ConfirmDelete_Message;
            ViewData["ID"] = id.ToString();
            return View("Confirm");
        }

        //
        // Post: /Section/Staff/Delete
        [HttpPost]
        [Authorize(Roles = "C.U")]
        public void Delete(FormCollection fc)
        {
            int id = int.Parse(fc["ID"]);
            sr.DeleteStaffMember(id);
            sr.Save();
        }

        //
        // GET: /Section/Staff/Edit/0000
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Edit(string systemName, int id)
        {
            // Redirect to admin section
            string adminSection = UsersRepository.GetAdminSection(systemName);
            UserType ut = UsersRepository.GetUserType(systemName);
            if (systemName != adminSection && ut != UserType.Administrator)
            {
                return RedirectToAction("Edit", new { systemName = adminSection, ID = id });
            }
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            tSite_Staff member = sr.GetStaffMemberByID(id);

            // Meta Tag
            string section = ser.GetSectionNameBySystemName(systemName).ToLower();
            ViewData["PageTitle"] = Resources.Staff_Edit_Title.Replace("[USERNAME]", member.Firstname + " " + member.Lastname);
            ViewData["PageDescription"] = Resources.Staff_Edit_Description;

            //Breadcrumb
            string[] saBreadcrumb = new String[3];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section), "SectionIndex", "Index", "Staff", new RouteValueDictionary(new { systemName = systemName }), null);
            saBreadcrumb[2] = Resources.Staff_Edit_Breadcrumb.Replace("[USERNAME]", member.Firstname + " " + member.Lastname);
            ViewData["Breadcrumb"] = saBreadcrumb;

            ViewData["SlDateIn"] = new SelectList(GetListYear(false), "Key", "Value", member.DateIn.ToString());
            ViewData["SlDateOut"] = new SelectList(GetListYear(true), "Key", "Value", member.DateOut.ToString());
            ViewData["SectionName"] = systemName;
            ViewData["MaxRequestLength"] = Files.GetMaxRequestLength();
            ViewData["FormsToken"] = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewData["DefaultImageID"] = Guid.NewGuid().ToString();

            if(member.Image != null)
            {
                byte[] baImage = Files.FileToArray(Server.MapPath(ConfigurationManager.AppSettings["StaffPath"] + member.Image + ".jpg"));
                HttpContext.Session["baImageStaff"] = baImage;
                ViewData["ImageStaff"] = baImage;
            }
            else
            {
                ViewData["ImageStaff"] = null;
            }

            return View(member);
        }

        //
        // Post: /Section/Staff/Edit/0000
        [HttpPost]
        [Authorize(Roles = "Animateur,C.U")]
        public ActionResult Edit(string systemName, int id, FormCollection fc)
        {
            // Redirect to admin section
            string adminSection = UsersRepository.GetAdminSection(systemName);
            UserType ut = UsersRepository.GetUserType(systemName);
            if (systemName != adminSection && ut != UserType.Administrator)
            {
                return RedirectToAction("Edit", new { systemName = adminSection, ID = id });
            }
            ViewData["UserType"] = ut;
            ViewData["SystemName"] = systemName;

            tSite_Staff member = sr.GetStaffMemberByID(id);

            // Meta Tag
            tSite_Section section = ser.GetSectionsBySystemName(systemName);
            ViewData["PageTitle"] = Resources.Staff_Edit_Title.Replace("[USERNAME]", member.Firstname + " " + member.Lastname);
            ViewData["PageDescription"] = Resources.Staff_Edit_Description;

            //Breadcrumb
            string[] saBreadcrumb = new String[3];
            saBreadcrumb[0] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Master_Breadcrumb_Home, "Default", "Index", "Home", null, null);
            saBreadcrumb[1] = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, RouteTable.Routes, Resources.Staff_Index_Breadcrumb.Replace("[SECTION]", section.Section.ToLower()), "SectionIndex", "Index", "Staff", new RouteValueDictionary(new { systemName = systemName }), null);
            saBreadcrumb[2] = Resources.Staff_Edit_Breadcrumb.Replace("[USERNAME]", member.Firstname + " " + member.Lastname);
            ViewData["Breadcrumb"] = saBreadcrumb;

            if (ValidateNewStaffMember(fc))
            {
                Guid? imageId = null;

                if (HttpContext.Session["baImageStaff"] != null)
                {
                    imageId = (member.Image != null) ? (Guid)member.Image : Guid.NewGuid();
                    string imagePath = this.ControllerContext.HttpContext.Server.MapPath(ConfigurationManager.AppSettings["StaffPath"] + imageId + ".jpg");
                    if (member.Image != null)
                    {
                        System.IO.File.Delete(imagePath);
                    }
                    System.IO.File.WriteAllBytes(imagePath, (byte[])HttpContext.Session["baImageStaff"]);
                }
                else if (member.Image != null)
                {
                    string imagePath = this.ControllerContext.HttpContext.Server.MapPath(ConfigurationManager.AppSettings["StaffPath"] + member.Image + ".jpg");
                    System.IO.File.Delete(imagePath);
                }

                member.ID = id;
                member.Image = imageId;
                member.IsModerator = (fc["isModerator"] == "1") ? true : false;
                member.Lastname = fc["lastname"];
                member.Mobile = fc["mobile"];
                member.Phone = fc["phone"];
                member.Sex = char.Parse(fc["sex"]);
                member.SID = section.ID;
                member.Totem = fc["totem"];
                member.Firstname = fc["firstname"];
                member.Email = fc["email"];
                member.DateIn = int.Parse(fc["SlDateIn"]);
                if (fc["SlDateOut"] != "0")
                {
                    member.DateOut = int.Parse(fc["SlDateOut"]);
                }
                sr.Save();

                ViewData["MID"] = member.ID;
                ViewData["ValidationMessage"] = Resources.Staff_Edit_Validation.Replace("[USERNAME]", "<strong>" + member.Firstname + " " + member.Lastname + "</strong>");
                HttpContext.Session["baImageStaff"] = null;

                List<tSite_Staff> staff = sr.GetStaffBySystemName(systemName);
                staff.Remove(member);
                return View("Validation", staff);
            }
            
            ViewData["SlDateIn"] = new SelectList(GetListYear(false), "Key", "Value");
            ViewData["SlDateOut"] = new SelectList(GetListYear(true), "Key", "Value");
            ViewData["SectionName"] = systemName;
            ViewData["ImageStaff"] = HttpContext.Session["baImageStaff"];
            ViewData["FormsToken"] = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewData["MaxRequestLength"] = Files.GetMaxRequestLength();
            ViewData["DefaultImageID"] = Guid.NewGuid().ToString();

            return View(member);
        }

        //
        // GET: /Section/Staff/Upload
        public UploadPicturesResult Upload(HttpPostedFileBase fileData, Guid ID, string token)
        {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
            if (ticket != null)
            {
                var identity = new FormsIdentity(ticket);
                if (identity.IsAuthenticated)
                {
                    string folder = ConfigurationManager.AppSettings["TempPath"] + "/";
                    string imageName = ID.ToString() + ".jpg";

                    return new UploadPicturesResult(fileData, folder, imageName, ImageMode.StaffCrop);
                }
            }
            return null;
        }

        //
        // GET: /Section/Staff/PublicStaffImageResize
        [HandleError]
        [OutputCache(CacheProfile="Photo")]
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public FileResult PublicStaffImageResize(Guid ID, string title)
        {
            byte[] baImage = Imaging.GetResizedImage(ID, ImageMode.Staff, false);

            return new FileContentResult(baImage, "image/jpeg");
        }

        //
        // GET: /Section/Staff/AdminStaffImageResize
        [Authorize(Roles = "Animateur,C.U")]
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public FileResult AdminStaffImageResize(Guid? ID)
        {
            // Image a load from file disk because the issue from flahs to store the image in the same session
            // Resized image doesn't need ti be cached because it can be deleted by user, if staff member not validated
            string filePath = this.Server.MapPath(ConfigurationManager.AppSettings["TempPath"] + "/") + ID.ToString() + ".jpg";
            byte[] baImage = null;

            if (System.IO.File.Exists(filePath))
            {
                baImage = System.IO.File.ReadAllBytes(filePath);
                Session["baImageStaff"] = baImage;

                Dictionary<string, int> dImageSize = Imaging.GetImageSize(ImageMode.StaffCrop);
                baImage = Imaging.ResizeImage(baImage, dImageSize["W"], dImageSize["H"], 100);
                System.IO.File.Delete(filePath);
            }
            else
            {
                baImage = (byte[])Session["baImageStaff"];
            }

            return new FileContentResult(baImage, "image/jpeg");
        }

        //
        // GET: /Section/Staff/AdminStaffImageCrop
        [Authorize(Roles = "Animateur,C.U")]
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public ActionResult AdminStaffImageCrop(string systemName, Guid ID)
        {
            ViewData["SystemName"] = systemName;
            ViewData["DefaultImageId"] = ID;

            return View("ImageCrop");
        }

        //
        // Post: /Section/Staff/AdminStaffImageCrop
        [HttpPost]
        [Authorize(Roles = "Animateur,C.U")]
        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        public void AdminStaffImageCrop(string systemName, FormCollection fc)
        {
            byte[] baImage = (byte[])Session["baImageStaff"];

            int w = -1;
            int h = -1;
            int x = -1;
            int y = -1;

            if (int.TryParse(fc["W"].ToString(), out w) && int.TryParse(fc["H"].ToString(), out h) && int.TryParse(fc["X"].ToString(), out x) && int.TryParse(fc["Y"].ToString(), out y))
            {
                baImage = Imaging.Crop(baImage, w , h , x , y);
            }
            Dictionary<string,int> dSize = Imaging.GetImageSize(ImageMode.Staff);
            baImage = Imaging.ResizeImage(baImage, dSize["W"], dSize["H"], 100);

            Session["baImageStaff"] = baImage;
        }

        //
        // Post: /Section/Staff/ClearImageCrop
        [HttpPost]
        public void ClearImageCrop()
        {
            Session["baImageStaff"] = null;
        }
        #endregion

        #region Methodes
        private Dictionary<int,string> GetListYear(bool blankDate)
        {
            int startYear = DateTime.Now.GetNowTimeByTimeZone().AddYears(-8).Year;
            int endYear = DateTime.Now.GetNowTimeByTimeZone().AddYears(1).Year;
            Dictionary<int, string> lYear = new Dictionary<int, string>();
            if (blankDate)
                lYear.Add(0, string.Empty);
            for (int i = startYear; i <= endYear; i++)
            {
                lYear.Add(i, i.ToString());
            }
            return lYear;
        }

        private bool ValidateNewStaffMember(FormCollection fc)
        {
            
            ModelState.SetModelValue("lastname", ValueProvider.GetValue("lastname"));
            ModelState.SetModelValue("firstname", ValueProvider.GetValue("firstname"));
            
            if (fc["SlDateOut"] != null && fc["SlDateOut"].ToString() != "0")
            {
                ModelState.SetModelValue("SlDateOut", ValueProvider.GetValue("SlDateOut"));
                
                if (int.Parse(fc["SlDateIn"].ToString()) > int.Parse(fc["SlDateOut"].ToString()))
                {
                    ModelState.AddModelError("SlDateOut", Resources.Staff_Edit_Error_DateOut);
                }
            }

            if (String.IsNullOrEmpty(fc["lastname"]))
            {
                ModelState.AddModelError("lastname", Resources.Staff_Create_Error_LastName);
            }
            if (String.IsNullOrEmpty(fc["firstname"]))
            {
                ModelState.AddModelError("firstname", Resources.Staff_Create_Error_FirstName);
            }
            return ModelState.IsValid;
        }
        #endregion
    }
}
